package com.baidu.service.impl;

import com.baidu.common.bean.User;
import com.baidu.common.exception.WebException;
import com.baidu.common.util.Md5Util;
import com.baidu.dao.UserDao;
import com.baidu.dao.impl.UserDaoImpl;
import com.baidu.service.UserService;

import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/19/20:04
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    public UserServiceImpl() throws WebException {
    }

    @Override
    public User get(Integer id) throws WebException {
        return userDao.get(id);
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int add(User user) throws WebException {
        user.setPassword(Md5Util.getMd5Str(user.getPassword()));
        return userDao.add(user);
    }

    @Override
    public User getByLogin(String userName, String password, String sql)throws WebException {
        String encryptedPassword = Md5Util.getMd5Str(password);

        String sqlStatus = "sql注入";

        User user;

        if (sqlStatus.equals(sql)){
            user = userDao.getByLoginSql(userName,encryptedPassword);
        }else{
            user = userDao.getByLoginNsql(userName,encryptedPassword);
        }

        return user;
    }

    @Override
    public List<User> queryGoodsTypeList() throws WebException {
        return userDao.queryGoodsTypeList();
    }

    @Override
    public User getUserByAccount(String account) throws WebException {
        return userDao.getUserByAccount(account);
    }
}
